//
//  SimpleNetworkTests.swift
//  SimpleNetworkTests
//
//  Created by 李龙 on 15/3/3.
//  Copyright (c) 2015年 Lauren. All rights reserved.
//

import UIKit
import XCTest

class SimpleNetworkTests: XCTestCase {
    
    /// 测试网络工具类
    let net  = SimpleNetWork()
    /// 测试网络地址
    let urlString = "http://httpbin.org/get"
    
    
    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }
    
    
    //测试requsetJSON
    //异步的单元测试
    ///  请求JSON － 网络访问是异步的，在 Xcode 6.0 之前，异步的单元测试相当的蛋疼
    ///  在 Xcode 4.0 的时候，苹果都不好意思把单元测试暴露出来！
    func testRequsetJSON() {
        //用期望来测试 异步测试
        let expectation = expectationWithDescription(urlString)
        
        net.requestJSON(HTTPMethod.GET, urlString, nil) { (result, error) -> () in
            println(result)
            // 标记 期望达成
            expectation.fulfill()
        }
        
        //等待期望达成
        waitForExpectationsWithTimeout(10.0, handler: { (error) -> Void in
            XCTAssertNil(error)
        })
        
    }
    
    //测试POST请求的构造
    func testPostRequest() {
        var request  = net.request(HTTPMethod.POST, urlString, nil)
        XCTAssertNil(request, "请求应该为空")
        
        request  = net.request(HTTPMethod.POST, urlString, ["name" : "zhang"])
        //测试请求方法
        XCTAssert(request!.HTTPMethod == "POST", "请求 不应该为空")
        // 测试数据体
        XCTAssert(request!.HTTPBody! == "name=zhang".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true), "数据体不正确")
        
    }
    
    //测试GET请求的构造
    func testGetRequest() {
        var request = net.request(HTTPMethod.POST, "", nil)
        XCTAssertNil(request, "请求应该为空")
        
        request = net.request(HTTPMethod.GET, "", nil)
        XCTAssertNil(request, "请求应该为空")
        
        request = net.request(HTTPMethod.GET, urlString, nil)
        XCTAssertNotNil(request, "请求应该被建立")
        XCTAssert(request!.URL!.absoluteString == urlString , "返回的 URL 不正确")
        
        request = net.request(HTTPMethod.GET, urlString, ["name" : "zhangsan"])
        XCTAssert(request!.URL!.absoluteString == urlString + "?name=zhangsan" , "返回的 URL 不正确")
    }
    
    
   //查询请求字符串
    func testQueryString() {
        XCTAssertNil(net.queryString(nil), "查询参数应该为空")
        println(net.queryString(["name" : "zhangsan"]))
        XCTAssert(net.queryString(["name" : "zhangsan"])! == "name=zhangsan")
        println(net.queryString(["name" : "zhangsan","title" : "boss"]))
        XCTAssert(net.queryString(["name" : "zhangsan","title" : "boss"])! == "title=boss&name=zhangsan")
        
        //测试百分号转译
        println(net.queryString(["name" : "zhangsan","book" : "ios 8.0"]))
        //这里空格也需要转译 空格转译之后是 %20
        XCTAssert(net.queryString(["name" : "zhangsan","book" : "ios 8.0"])! == "book=ios%208.0&name=zhangsan")
        
    }
    
}
